package com.hantanger.weblog.dao;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.objectify.cmd.Query;
import com.hantanger.weblog.model.Post;
import com.hantanger.weblog.util.DaoHelper;

public class PostDao {

	public void update(Post post) {
		DaoHelper.save(post);
	}

	public void delete(long postId) {
		DaoHelper.delete(Post.class, postId);
	}

	public Post getPostById(long postId) {
		return DaoHelper.get(Post.class, postId);
	}

	public List<Post> getPostsByCategory(long categoryId, int limit,
			long publishTime, boolean publicOnly) {
		if(categoryId == -1){
			return getPosts(limit, publishTime, publicOnly);
		}else if(categoryId == -2){
			return getDraftPosts(limit, publishTime);
		}
		
		Query<Post> query = DaoHelper.getLoader().type(Post.class)
				.filter("categoryId", categoryId).filter("published", true);
		if (publicOnly) {
			query.filter("publiced", true);
		}
		query.filter("publishTime <", publishTime).limit(limit).order("-publishTime");
		List<Post> posts = new ArrayList<Post>();
		for (Post post : query) {
			posts.add(post);
		}
		return posts;
	}
	
	public List<Post> getPostsByTagName(String tagName, int limit,
			long publishTime, boolean publicOnly) {
		tagName = tagName.toLowerCase();
		Query<Post> query = DaoHelper.getLoader().type(Post.class)
				.filter("tags", tagName);
		if (publicOnly) {
			query.filter("publiced", true).filter("published", true);
		}
		query.filter("publishTime <", publishTime).limit(limit).order("-publishTime");
		List<Post> posts = new ArrayList<Post>();
		for (Post post : query) {
			posts.add(post);
		}
		return posts;
	}

	public List<Post> getPostsByArchive(long archiveDate, int limit,
			long publishTime, boolean publicOnly) {
		Query<Post> query = DaoHelper.getLoader().type(Post.class)
				.filter("archiveDate", archiveDate);
		if (publicOnly) {
			query.filter("publiced", true).filter("published", true);
		}
		query.filter("publishTime <", publishTime).limit(limit).order("-publishTime");
		List<Post> posts = new ArrayList<Post>();
		for (Post post : query) {
			posts.add(post);
		}
		return posts;
	}

	public List<Post> getPosts(int limit, long publishTime, boolean publicOnly) {
		Query<Post> query = DaoHelper.getLoader().type(Post.class);
		if (publicOnly) {
			query.filter("publiced", true).filter("published", true);
		}
		query.filter("publishTime <", publishTime).limit(limit).order("-publishTime");
		List<Post> posts = new ArrayList<Post>();
		for (Post post : query) {
			posts.add(post);
		}
		return posts;
	}

	public List<Post> getDraftPosts(int limit, long publishTime) {
		Query<Post> query = DaoHelper.getLoader().type(Post.class)
				.filter("published", false);
		query.filter("publishTime <", publishTime).limit(limit).order("-publishTime");
		List<Post> posts = new ArrayList<Post>();
		for (Post post : query) {
			posts.add(post);
		}
		return posts;
	}

	public List<Post> getPrevNextPosts(long publishTime, boolean publicOnly) {
		Query<Post> query = DaoHelper.getLoader().type(Post.class);
		if (publicOnly) {
			query.filter("publiced", true).filter("published", true);
		}
		//下一篇，较早发表的日志
		query.filter("publishTime <", publishTime).limit(1).order("-publishTime");
		List<Post> posts = new ArrayList<Post>();
		for (Post post : query) {
			posts.add(post);
		}
		if(posts.size() == 0){
			posts.add(null);
		}
		
		query = DaoHelper.getLoader().type(Post.class).filter("published", true);
		if (publicOnly) {
			query.filter("publiced", true);
		}
		//上一篇，较晚发表的日志
		query.filter("publishTime >", publishTime).limit(1).order("publishTime");
		for (Post post : query) {
			posts.add(post);
		}
		if(posts.size() == 1){
			posts.add(null);
		}
		return posts;
	}

}
